<?php

namespace app\api\controller\v1;

use app\api\controller\Fishinggroundpositions as ControllerFishinggroundpositions;
use app\api\model\wanlshop\Auth;
use app\common\controller\Shopapi;
use app\common\model\FishingGround;
use app\common\model\FishingGroundFishs;
use app\common\model\FishingGroundPositions;
use app\common\model\FishingGroundTypes;
use fast\Random;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 塘主
 */
class Ground extends Shopapi
{
    protected $noNeedLogin =['login'] ;
    protected $noNeedRight = ['*'];

    //新建钓场
    public function add_ground(){
        $shop = $this->shopauth->getUser();
        //关联审核信息
        $auth = Auth::where('id',$shop->ass_id)->find();
        $params = $this->request->post();
       
        $is  = FishingGround::where('name',$params['name'])->find();
        if($is){
            $this->error('钓场名称已被使用');
        }
        $data = [
            'name' => $params['name'],
            'province' => $auth->province,
            'city' => $auth->city,
            'district' => $auth->district,
            'address' => $auth->address,
            'lon' => $shop->lat,
            'lat' => $shop->lng,
            'source_type' => 2,
            'source_id' => $shop->id,
            'identity' => 0,

        ];
        FishingGround::create($data);

        $this->success('成功');


		

    }

     //我的钓场列表
	 public function my_ground(){
		$shop = $this->shopauth->getUser();
		
		$list = FishingGround::where('source_id',$shop->id)->where('source_type',2)->select();

		$this->success('成功',$list);

	 }
     //鱼塘类型列表
     public function fishing_ground_types(){
        $fishing_ground_types = Db::name('config')->where('name','fishing_ground_types')->value('value');
        $fishing_ground_types = json_decode($fishing_ground_types,true);
        $data = [];
        foreach($fishing_ground_types as $k=>$v){
            $res['id'] = $k;
            $res['name'] = $v;
            $data[] = $res;
            
        }
        $this->success('成功',$data);
     }

     //鱼钟类型列表
     public function fishing_yu(){
        $fish_typess = Db::name('config')->where('name','fish_types')->value('value');
        $fish_typess = json_decode($fish_typess,true);
        $data = [];
        foreach($fish_typess as $k=>$v){
            $res['id'] = $k;
            $res['name'] = $v;
            $data[] = $res;
            
        }
        $this->success('成功',$data);
     }


     //增加钓位选的本塘鱼种
     public function fishing_tang_yu(){
        $params = $this->request->post();
        $data =  FishingGroundFishs::where('types_id',$params['types_id'])->order('id desc')->select();
        foreach($data as $k=>$v){
            $data[$k]['type_name'] = $this->yu_id_name($v['type']);
        }
       
        $this->success('成功',$data);
     }

     //鱼种id 查鱼种姓名
     public function yu_id_name($id){
        $fish_typess = Db::name('config')->where('name','fish_types')->value('value');
        $fish_typess = json_decode($fish_typess,true);
        $data = [];
        foreach($fish_typess as $k=>$v){
            if($id == $k){
                return $v;
            }
        }
     }
     //新建鱼塘
     public  function add_type(){
        $shop = $this->shopauth->getUser();
        $params = $this->request->post();
        $ground =  FishingGround::where('id',$params['ground_id'])->find();
        if(empty($ground)){
            $this->error('钓场不存在');
        }
      
        if(FishingGroundTypes::where('name',$params['name'])->find()){
            $this->error('鱼塘名称已存在');
        }
        $yu_ids = explode(',',$params['yu_type']);
        $data = $params;
        unset($data['yu_type']);
        unset($data['positions']);
        $data['createtime'] = time();
        $data['updatetime'] = time();
        

        // $params['positions'] = [
        //     ['fishs_id'=>1,'name'=>'钓位一号'],
        //     ['fishs_id'=>2,'name'=>'钓位二号']
        // ];
        Db::startTrans();
        try{
            $tang_id = Db::name('fishing_ground_types')->insertGetId($data);
            foreach($yu_ids as $k=>$v){
                $res = [
                    'ground_id' => $params['ground_id'],
                    'types_id' => $tang_id,
                    'type' => $v,
                ];
               $re =  FishingGroundFishs::create($res);
            }
          
           Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        if($re){
          //增加钓位信息
          $new = new FishingGroundPositions;

          foreach($params['positions'] as $k=>$v){
              $is = $new->where('name',$v['name'])->find();
              if($is){
                  $diaoweiname = $v['name'];
                  $this->error("您所上传的钓位名称$diaoweiname'.'已存在");
              }

          }
          $new->saveAll($params['positions']);

        }
        $this->success('添加成功');
     }

     //我的鱼塘
     public  function  my_yu_tang(){
        $params = $this->request->post();
        $yutanglist = FishingGroundTypes::where('ground_id',$params['ground_id'])->order('createtime desc')->select();
      
        if($yutanglist){
            foreach($yutanglist as $k=>$v){
                //鱼种
               $fishing = FishingGroundFishs::where(['ground_id'=>$params['ground_id'],'types_id'=>$v['id']])->column('type');
               $name = [];
               foreach($fishing as $vv){
                $name[] = $this->yu_id_name($vv);
               }
               $yutanglist[$k]['yu_name'] = implode('、',$name);


               //算钓位
               $fishs_ids = FishingGroundFishs::where('ground_id',$params['ground_id'])->where('types_id',$v['id'])->column('id');
               $diaowei = FishingGroundPositions::where('fishs_id','in',$fishs_ids)->select();
               $yutanglist[$k]['diaowei'] = $diaowei;

            }

        }
        $this->success('成功',$yutanglist);
        
     }

 



   

}